﻿using System;
using System.Text;
using System.Windows.Forms;

namespace Project.Co_DataTransformationPrototype.UI
{
    public partial class ScriptViewerForm : Form
    {
        private StringBuilder sqlScriptBuilder = new StringBuilder();
        //private MappingUtilities mappingUtilities;

        public ScriptViewerForm(StringBuilder sqlScriptBuilder)
        {
            InitializeComponent();
            //mappingUtilities = MappingUtilities.GetInstance();
            //mappingUtilities.ScriptUpdated += update_rbtScript;
            this.sqlScriptBuilder = sqlScriptBuilder;
        }

        private void ScriptViewerForm_Load(object sender, EventArgs e)
        {
            this.rtbScript.Text = sqlScriptBuilder.ToString();
        }

        //private void update_rbtScript(object sender, ScriptEventArgs e)
        //{
        //    sqlScriptBuilder = e.scriptCollection;
        //}

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (fldrdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    // TODO: use custom filebrowser to allow users to specify file name
                    System.IO.File.WriteAllText(fldrdlg.SelectedPath + @"\script.sql", rtbScript.Text);
                    MessageBox.Show("SQL Script file created", "SQL Script Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("SQL Script file creation failure: " + ex.Message, "SQL Script Creation", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {            
            this.Hide();
        }
    }
}
